//
//  CZStatusOriginalView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusOriginalView: UIView {
    // 原创微博的底部约束记录
    var originalViewBottomConstraint:Constraint?
    
    
    // 定义一个ViewModel 供外界赋值
    var statusUserViewModel:CZStatusUserViewModel?{
        
        didSet{
            
            messageLabel.text = statusUserViewModel?.statusModel?.text
           headImage.cz_setImageWithString(statusUserViewModel?.statusModel?.user?.profile_image_url, playholderImageName: "avatar_default")
            nameLabel.text = statusUserViewModel?.statusModel?.user?.name
            //时间
            timeLabel.text = statusUserViewModel?.sinaTimeStr
            //微博正文
          messageLabel.attributedText = statusUserViewModel?.originalAttr
            
            
            sourceLabel.text = statusUserViewModel?.sinaSourceStr
            
            //用户vip等级的图片
            vipImageView.image = statusUserViewModel?.vipImage
            //认证图片
            avatarImageView.image = statusUserViewModel?.verifiedImage
            
            
            // 卸载约束
            originalViewBottomConstraint?.uninstall()
            
            if statusUserViewModel?.statusModel?.pic_urls?.count > 0 {
                //代表有配图
                
                //赋值
                picktureView.pictureModel = statusUserViewModel?.statusModel?.pic_urls
                
                self.snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(picktureView).offset(10).constraint
                }
                // 显示配图
                picktureView.hidden = false
            }else{
                //没有配图
                self.snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(messageLabel).offset(10).constraint
                }
                //隐藏配图
                picktureView.hidden = true
                
            }
            
            
        }
        
    }
    
    
    //处理微博内容富文本
    func sinaDealContentAttr(text:String)->NSMutableAttributedString{
    //把当前字符串转成富文本
        let allTextAttr = NSMutableAttributedString(string: text)
        
//        print(text)
        //正则匹配的规则
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        /**
         *  正则匹配的规则
         *
         *  @param NSString 正则匹配的规则
         - 匹配的个数
         - 匹配的结果
         - 匹配的范围
         - 匹配是否停止
         *
         *  @return
         */

        //创建可变数组
        var matchArray:[CZMatchModel] = [CZMatchModel]()
        
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, result, range, _) in
//            print(result.memory, range.memory)
            //实例化model
            let model = CZMatchModel(string: result.memory as! String, range: range.memory)
            //添加模型到数组中
            matchArray.append(model)
 }
        //倒着遍历数组
        for matchmodel in matchArray.reverse() {
            //判断是否为nil
            guard let emoticonModel = CZEmoticonTools.shareTools.searchEmoticonDefaultEmoticonOrLxhEmoticons(matchmodel.string) else{
                continue
            }
            
            //创建一个文字附件
            let att = NSTextAttachment()
            //设置image
            att.image = UIImage.emoticonsImage(emoticonModel)
            //行高
            let lineHeight = UIFont.systemFontOfSize(15).lineHeight
            //设置bounds
            att.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            //创建一个不可变的富文本
            let attr = NSAttributedString(attachment: att)
            //替换
            allTextAttr.replaceCharactersInRange(matchmodel.range, withAttributedString: attr)
            }
        
        
        
        //返回一个替换完成的富文本
        return allTextAttr
        
    }
    
    
 
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
       backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(messageLabel)
        addSubview(picktureView)
        
        //设置约束
        headImage.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(10)
            make.size.equalTo(CGSize(width: 35,height: 35))
        }
        
        
        nameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(headImage.snp_trailing).offset(10)
            make.top.equalTo(headImage.snp_top)
        }
        
        
        vipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImage.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(headImage.snp_bottom)
            make.centerX.equalTo(headImage.snp_trailing)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImage.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }
        
        //设置原创微博图片的约束
        picktureView.snp_makeConstraints { (make) in
            //在自己的view里面更新约束
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
              make.leading.equalTo((SCREENW - 250)/2)
        }
        
        
        //设置原创微博的View 显示的范围
        self.snp_makeConstraints { (make) in
             originalViewBottomConstraint = make.bottom.equalTo(picktureView).offset(10).constraint
        }
       
        
    }
    
    
    
    
    //懒加载控件
    //头像
    private lazy var headImage:UIImageView = UIImageView(imageName: "avatar_default")
    //名字
    private lazy var nameLabel:UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    //用户VIP 等级
    private lazy var vipImageView:UIImageView = UIImageView(imageName: "common_icon_membership_expired")
    
    
    //时间
    private lazy var timeLabel:UILabel = UILabel(textColor: AppThemeColor, textSize: 10)
    
    //来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 10)
    
    //认证
    private lazy var avatarImageView:UIImageView = UIImageView(imageName: "avatar_vgirl")
    
    //微博正文
    private lazy var messageLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW-20)
    
    //原创图片
    private lazy var picktureView:CZStatusPicktureView = CZStatusPicktureView()
    
}







